﻿namespace Sai.Core
{
	using System.Collections.Generic;

	public interface IExactlyMatch<T>
	{
		bool ExactMatchFor(T other);
	}

	public static class ExactMatcher
	{
		public static bool ContainsExactMatch<T>(this IEnumerable<T> collection, T matchCandidate)
							where T : IExactlyMatch<T>
		{
			bool isExactMatch = false;

			foreach (T item in collection)
			{
				if (item.ExactMatchFor(matchCandidate))
				{
					isExactMatch = true;
					break;
				}
			}

			return isExactMatch;
		}

        public static T ExactMatchFor<T>(this IEnumerable<T> collection, T matchCandidate)
                    where T : IExactlyMatch<T>
        {
            T matched = default(T);

            foreach (T item in collection)
            {
                if (item.ExactMatchFor(matchCandidate))
                {
                    matched = item;
                    break;
                }
            }

            return matched;
        }

	}
}
